package com.rfid.afd.activity;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.rfid.afd.activity.adapter.StatisticsAdapter;
import com.rfid.afd.activity.view.IStatisticsView;
import com.rfid.afd.model.StatisticsModel;
import com.rfid.afd.presenter.StatisticsPresenter;
import com.rfid.db.bean.Rfid;
import com.rfid.db.bean.User;
import com.rfid.rls_psw.R;
import com.rfid.util.DBManager;
import com.rfid.util.UserManager;
import com.rfid.util.Util;
import com.rfid.widget.LoadingView;
import com.rfid.widget.ShapeLoadingView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
/**统计，用于统计操作人员进行的操作，列出操作到的数据。*/
public class StatisticsActivity extends BaseActivity<StatisticsPresenter> implements IStatisticsView {

    @BindView(R.id.date_picker_start)
    EditText mDataPickStart;
    @BindView(R.id.date_picker_end)
    EditText mDataPickEnd;
    @BindView(R.id.user)
    Spinner mUsers;
    @BindView(R.id.kind)
    Spinner mKind;
    @BindView(R.id.count)
    TextView mCount;

    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    @BindView(R.id.loading)
    LoadingView mLoadingView;

    StatisticsAdapter myAdapter;
    private List<StatisticsModel.Statistics> mStatistics;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_statisticsc);
        ButterKnife.bind(this);
        mDataPickStart.setText(Util.getDate());
        mDataPickEnd.setText(Util.getDate());
        List<User> users = UserManager.getInstance().getUsers();
        mUsers.setAdapter(new ArrayAdapter<>(this,android.R.layout.simple_spinner_dropdown_item, users));
        int position = users.indexOf(UserManager.getInstance().getLoginUser());
        mUsers.setSelection(position);
        initDataPick();
        mStatistics = new ArrayList<>();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this,RecyclerView.VERTICAL));
        myAdapter = new StatisticsAdapter(this,mStatistics);
        mRecyclerView.setAdapter(myAdapter);
        LayoutAnimationController controller = new LayoutAnimationController(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
        mRecyclerView.setLayoutAnimation(controller);
    }

    private void initDataPick() {
        mDataPickStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePicker datePicker = new DatePicker(StatisticsActivity.this);
                AlertDialog dialog = new AlertDialog.Builder(StatisticsActivity.this).create();
                dialog.setTitle(R.string.select_date);
                dialog.setView(datePicker);
                dialog.setButton(DialogInterface.BUTTON_POSITIVE, getString(R.string.yes), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String y = datePicker.getYear() < 10 ? "0" + datePicker.getYear() : datePicker.getYear() + "";
                        String m = datePicker.getMonth() + 1 < 10 ? "0" + (datePicker.getMonth() + 1) : (datePicker.getMonth() + 1) + "";
                        String d = datePicker.getDayOfMonth() < 10 ? "0" + datePicker.getDayOfMonth() : datePicker.getDayOfMonth() + "";

                        String show = y + "/" + m + "/" + d;
                        if(show.compareTo(mDataPickEnd.getText().toString()) > 0) {
                            Toast.makeText(StatisticsActivity.this,R.string.select_start_date_err,Toast.LENGTH_SHORT).show();
                        } else {
                            mDataPickStart.setText(show);
                        }
                    }
                });
                dialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.no), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog.show();
            }
        });
        mDataPickEnd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePicker datePicker = new DatePicker(StatisticsActivity.this);
                AlertDialog dialog = new AlertDialog.Builder(StatisticsActivity.this).create();
                dialog.setTitle(R.string.select_date);
                dialog.setView(datePicker);
                dialog.setButton(DialogInterface.BUTTON_POSITIVE, getString(R.string.yes), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String y = datePicker.getYear() < 10 ? "0" + datePicker.getYear() : datePicker.getYear() + "";
                        String m = datePicker.getMonth() + 1 < 10 ? "0" + (datePicker.getMonth() + 1) : (datePicker.getMonth() + 1) + "";
                        String d = datePicker.getDayOfMonth() < 10 ? "0" + datePicker.getDayOfMonth() : datePicker.getDayOfMonth() + "";
                        String show = y + "/" + m + "/" + d;
                        if(show.compareTo(mDataPickStart.getText().toString()) < 0) {
                            Toast.makeText(StatisticsActivity.this,R.string.select_end_date_err,Toast.LENGTH_SHORT).show();
                        } else {
                            mDataPickEnd.setText(show);
                        }
                    }
                });
                dialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.no), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog.show();
            }
        });
    }

    @Override
    public StatisticsPresenter getPresenter() {
        return new StatisticsPresenter();
    }

    public void search(View view) {
        String user = ((User)mUsers.getSelectedItem()).getName();
        int operation = mKind.getSelectedItemPosition() + 1;//盘点2，定位1，偷懒了。补充：上架3，错架4
        String start = mDataPickStart.getText().toString() + " 00:00:00";
        String end = mDataPickEnd.getText().toString() + " 23:59:59";
        mPresenter.getStatistics(user,operation,start,end);
    }

    @Override
    public void onStaticsRfidGot(List<StatisticsModel.Statistics> rfids) {
        System.out.println("rfids size = " + rfids.size());
        mCount.setText(getString(R.string.statistics_count,rfids.size()));
        mStatistics.clear();;
        mStatistics.addAll(rfids);
        myAdapter.notifyDataSetChanged();
    }

    @Override
    public void onExport(float rate, StatisticsModel.Statistics statistics) {
        mStatistics.remove(statistics);
        myAdapter.notifyDataSetChanged();
        if(rate == 1) {
            Toast.makeText(this,"已完成",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void showLoading() {
        mLoadingView.setLoadingText(getString(R.string.searching));
        mLoadingView.setVisibility(View.VISIBLE);
    }

    @Override
    public void stopLoading() {
        mLoadingView.setVisibility(View.GONE);
//        mLoadingView.stopLoading();
    }

    public void export(View view) {
        mPresenter.export(mStatistics);
    }

    public void quit(View view) {
        onBackPressed();
    }
}